<script >
import {userUpdateService} from "@/api/user/UserService";
export default {
    data() {
        return {
            userInfo: [],
            form: {
            },
            imageUrl: '',
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { min: 11, max: 11, message: '手机号为11位数字', trigger: 'blur' }
                ]
            },
            flag: false
        }
    },
    methods: {
        handleAvatarSuccess(res, file) {
            this.$message({
                message: '头像上传成功',
                type: 'success'
            })
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;
            
            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        // 封装时间返回对象
        getFormatDate(time) {
            const date = new Date(time);
            const year = date.getFullYear();
            const month = (date.getMonth() + 1).toString().padStart(2, '0');
            const day = date.getDate().toString().padStart(2, '0');
            return `${year}-${month}-${day}`;
        },
        async UpdateUser() {
            let result = await userUpdateService(this.form);
            if (result.code === 200) {
                this.$message({
                    message: '修改成功',
                    type: 'success'
                })
                this.$store.commit('setUserInfo', this.form);
                this.form.birthday = this.getFormatDate(this.form.birthday);
                this.userInfo = JSON.parse(JSON.stringify(this.form));
                this.flag = false;
            } else {
                this.$message({
                    message: '修改失败',
                    type: 'error'
                })
            }
        }
    },
    created() {
        this.userInfo = JSON.parse(JSON.stringify(this.$store.getters.userInfo));
        this.form = JSON.parse(JSON.stringify(this.$store.getters.userInfo));
        console.log(this.form)
    }
}
</script>

<template>
    <div class="UserModel">
        <div class="MASK" v-show="flag">
            <div class="MaskContain">
                <el-form :rules="rules" label-position="left" ref="form" :model="form" label-width="80px">
                    <el-form-item label="头像">
                        <el-upload
                            class="avatar-uploader"
                            action="http://localhost:3310/user/upload"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                            <img style="width: 178px; height: 178px" v-if="form.avatar" :src="form.avatar" class="avatar" alt="">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="form.username"></el-input>
                    </el-form-item>
                    <el-form-item label="生日">
                        <el-date-picker
                            v-model="form.birthday"
                            type="date"
                            placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="性别">
                        <el-radio v-model="form.gender" label="0">女</el-radio>
                        <el-radio v-model="form.gender" label="1">男</el-radio>
                    </el-form-item>
                    <el-form-item label="手机号码" prop="phone">
                        <el-input v-model="form.phone"></el-input>
                    </el-form-item>
                    <el-form-item >
                        <el-button type="primary" @click="UpdateUser">保存</el-button>
                        <el-button @click="flag = false">取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <div class="u_top">
            <div>简介</div>
            <el-button type="primary" icon="el-icon-edit" @click="flag = true">修改</el-button>
        </div>
        <div class="u_body">
            <div class="u_b_left">
                <div class="Le">
                    <div class="U_img">
                        <i class="el-icon-picture" />
                        头像
                    </div>
                    <div class="U_name">
                        <i class="el-icon-user-solid" />
                        昵称
                    </div>
                    <div class="U_gender">
                        <i class="el-icon-male" />
                        <i class="el-icon-female" />
                        性别
                    </div>
                    <div class="U_phone">
                        <i class="el-icon-mobile-phone" />
                        手机号码
                    </div>
                    <div class="U_work">
                        <i class="el-icon-office-building" />
                        职业
                    </div>
                    <div class="U_signal">
                        <i class="el-icon-chat-line-round" />
                        个性签名
                    </div>
                </div>
                <div class="Re">
                    <div class="U_img">
                        <el-image style="width: 150px;height: 150px;border-radius: 50%;box-shadow: rgb(157 125 125) 0 0 13px 0;border: none;" :src="userInfo.avatar" />
                    </div>
                    <div class="U_name">
                        {{ userInfo.username }}
                    </div>
                    <div class="U_gender">
                        {{ userInfo.gender === "0" ? "女" : "男" }}
                    </div>
                    <div class="U_phone">
                        {{ userInfo.phone }}
                    </div>
                    <div class="U_work">
                        码农
                    </div>
                    <div class="U_signal">
                        JAVA是世界上最好的语言
                    </div>
                </div>
            </div>
            <div class="u_b_right">
                <div class="Le">
                    <div class="U_username">
                        <i class="el-icon-user" />
                        用户名
                    </div>
                    <div class="U_birth">
                        <i class="el-icon-bell" />
                        生日
                    </div>
                    <div class="U_email">
                        <i class="el-icon-message" />
                        邮箱Email
                    </div>
                    <div class="U_address">
                        <i class="el-icon-location-information" />
                        地区
                    </div>
                    <div class="U_like">
                        <i class="el-icon-s-opportunity" />
                        兴趣爱好
                    </div>
                    <div class="U_registerTime">
                        <i class="el-icon-s-promotion" />
                        注册日期
                    </div>
                </div>
                <div class="Re">
                    <div class="U_username">
                        {{ userInfo.username }}
                    </div>
                    <div class="U_birth">
                        {{ userInfo.birthday }}
                    </div>
                    <div class="U_email">
                        1955905622@qq.com
                    </div>
                    <div class="U_address">
                        江苏苏州
                    </div>
                    <div class="U_like">
                        写项目
                    </div>
                    <div class="U_registerTime">
                        {{ userInfo.registerTime }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped lang="scss">
.UserModel {
    width: 72%;
    height: 100%;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 0 7px 3px rgb(212 206 206);
    background-color: #fff;
    .MASK {
        position: fixed;
        width: 100vw;
        height: 100vh;
        z-index: 100;
        left: 0;
        top: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: rgba(239, 233, 226,0.7);
        .MaskContain {
            background-color: rgba(226, 231, 240, 0.99);
            .el-form {
                font-size: 20px;
                .el-form-item {
                    width: 380px;
                    display: flex;
                    justify-content: flex-start;
                    align-items: center;
                    .el-input {
                        width: 220px;
                    }
                }
                .el-form-item:nth-last-child(1) {
                    gap: 30px;
                }
                .el-form-item:nth-child(1) {
                    .avatar-uploader {
                        display: flex;
                        justify-content: center;
                        align-content: center;
                        background-color: rgba(127, 217, 194);
                        .el-upload {
                            border: 1px dashed #d9d9d9;
                            border-radius: 6px;
                            cursor: pointer;
                            position: relative;
                            overflow: hidden;
                            
                            .el-upload:hover {
                                border-color: #409EFF;
                            }
                        }
                        
                        .avatar-uploader-icon {
                            font-size: 28px;
                            color: #8c939d;
                            width: 178px;
                            height: 178px;
                            line-height: 178px;
                            text-align: center;
                        }
                        
                        .avatar {
                            width: 178px;
                            height: 1178px;
                            display: block;
                        }
                    }
                }
            }
        }
        
    }
    .u_top {
        width: 100%;
        height: 50px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 5px 5px 5px 10px;
        font-weight: 800;
        div {
            font-size: 18px;
        }
        .el-button {
            font-weight: 800;
            transition: all .5s ease;
            color: #a4c1fa;
            transform: scale(0.88);
            background-color: rgba(238, 245, 254,0.99);
        }
        .el-button:hover {
            color: #fff;
            background-color: rgba(100, 156, 248);
        }
    }
    .u_body {
        padding: 0 10px;
        width: 100%;
        height: 100%;
        display: grid;
        grid-template-columns: 1fr 1fr;
        .u_b_left,.u_b_right {
            width: 100%;
            height: 100%;
            display: grid;
            grid-template-columns: 4fr 5fr;
            .Le, .Re {
                width: 100%;
                height: 100%;
                font-size: 15px;
                div {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    background-color: #f5f3f0;
                    border-top: 1px solid #dadada;
                    border-left: 1px solid #dadada;
                    border-right: 1px solid #dadada;
                    gap: 5px;
                    font-weight: 800;
                }
                div:nth-child(n + 2) {
                    height: 49px;
                }
                div:nth-last-child(1) {
                    border-bottom: 1px solid #dadada;
                }
                .U_img {
                    width: 100%;
                    height: 180px;
                    font-size: 18px;
                    .el-image {
                        width: 175px;
                        height: 175px;
                    }
                }
            }
            .Re {
                color: #bec4f8;
            }
        }
        .u_b_right {
            .Le, .Re {
                .U_username {
                    width: 100%;
                    height: 180px;
                    font-size: 18px;
                }
            }
            .Re {
                color: #efaba9;
            }
        }
    }
}
</style>
